package com.warm.androidstudy.utils;

import android.content.Context;
import android.content.DialogInterface;

import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;

import com.karumi.dexter.Dexter;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.multi.CompositeMultiplePermissionsListener;
import com.karumi.dexter.listener.multi.MultiplePermissionsListener;
import com.warm.androidstudy.R;
import com.warm.androidstudy.widget.MyDialogOnAnyDeniedMultiplePermissionsListener;


/**
 * Created by shenyq on 2017/7/9.
 */

public class PermissionUtil {

    /**
     * 多个权限检测
     *
     * @param context          上下文
     * @param feedbackListener 监听
     * @param permissions      权限
     * @param describeMsg      权限名
     */
    public static void dexterMultipleCheck(Context context, MultiplePermissionsListener feedbackListener, String describeMsg, String... permissions) {
        if (!Dexter.isRequestOngoing()) {
            CompositeMultiplePermissionsListener compositeMultiplePermissionsListener =
                    new CompositeMultiplePermissionsListener(feedbackListener, PermissionUtil.getMultipleSnack(context, describeMsg));
            Dexter.checkPermissions(compositeMultiplePermissionsListener, permissions);
        }
    }

    /**
     * 权限批量申请
     *
     * @param context
     * @param describeMsg 权限名
     * @return
     */
    public static MyDialogOnAnyDeniedMultiplePermissionsListener getMultipleSnack(Context context, String describeMsg) {
        return MyDialogOnAnyDeniedMultiplePermissionsListener.Builder.withContext(context)
                .withTitle("需要" + describeMsg + "权限")
                .withMessage("没有" + describeMsg + "这个权限,部分应用的功能可能受限！")
                .withButtonText(android.R.string.ok)
                .build();
    }

    public static void onPermissionRationaleShouldBeShown(Context context, final PermissionToken token, String title) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("\n" + title);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                token.continuePermissionRequest();
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                token.cancelPermissionRequest();
                dialog.dismiss();
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.setCancelable(false); // 点击返回键不可取消
        alertDialog.show();
        //(解决在用的版本手机上看不到AlertDialog的按钮的问题)
        alertDialog.getButton(alertDialog.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(context, R.color.gray_f));//必须在show之后调用
        alertDialog.getButton(alertDialog.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(context, R.color.red_FF77A9));//必须在show之后调用
    }

    public static void onPermissionRationaleShouldBeShown(Context context, final PermissionToken token, String title, String msg) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(msg);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                token.continuePermissionRequest();
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                token.cancelPermissionRequest();
                dialog.dismiss();
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.setCancelable(false); // 点击返回键不可取消
        alertDialog.show();
        //(解决在用的版本手机上看不到AlertDialog的按钮的问题)
        alertDialog.getButton(alertDialog.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(context, R.color.gray_f));//必须在show之后调用
        alertDialog.getButton(alertDialog.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(context, R.color.red_FF77A9));//必须在show之后调用
    }
}


